<?php
/**
 * Created by PhpStorm.
 * User: yf
 * Date: 2018/5/28
 * Time: 下午6:33
 */

namespace EasySwoole\EasySwoole;


use EasySwoole\Component\Di;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\Http\Request;
use EasySwoole\Http\Response;
use Illuminate\Database\Capsule\Manager as DB;

class EasySwooleEvent implements Event
{

    public static function initialize()
    {
        // TODO: Implement initialize() method.
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        $instance = \EasySwoole\EasySwoole\Config::getInstance();
//
//        $db = new DB;
        // 单例模式
        Di::getInstance()->set('MYSQL', DB::class);
        $db = Di::getInstance()->get('MYSQL');
        $db->addConnection($instance->getConf('DATABASE'));
        // 设置全局静态可访问
        $db->setAsGlobal();
        // 启动Eloquent
        $db->bootEloquent();
    }

    public static function onRequest(Request $request, Response $response): bool
    {
        // TODO: Implement onRequest() method.
        return true;
    }

    public static function afterRequest(Request $request, Response $response): void
    {
        // TODO: Implement afterAction() method.
    }
}